home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / BLAZER.PAK / ABOUT.CPP next >
C/C++ Source or Header  |  1997-05-06  |  9KB  |  378 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1995 by Borland International, All Rights Reserved
  4. //
  5. // Implementation of the Help | About menu choice.
  6. //----------------------------------------------------------------------------
  7. #include <owl/pch.h>
  8. #include <owl/propsht.h>
  9. #include <owl/dibitmap.h>
  10. #include <owl/button.h>
  11. #include <owl/mci.h>
  12. #include <winsys/uimetric.h>
  13. #include "blazer.h"
  14. #include "propdlgs.h"
  15.  
  16. int RandomMax     = 4;
  17. int AbsoluteDelta = 7;
  18.  
  19. DEFINE_RESPONSE_TABLE1(TSpeedBallWindow, TWindow)
  20.   EV_WM_SIZE,
  21.   EV_WM_PALETTECHANGED,
  22.   EV_WM_QUERYNEWPALETTE,
  23. END_RESPONSE_TABLE;
  24.  
  25. //
  26. // TSpeedBallWindow Contructor (WinG bouncing ball)
  27. // Initializes variables.
  28. //
  29. TSpeedBallWindow::TSpeedBallWindow(TWindow* w)
  30. :
  31.   TWindow(w, 0),
  32.   ClientDC(0),
  33.   DisplayBitmap(0),
  34.   BlitterBlocks(true),
  35.   EnableSound(false),
  36.   Delay(1)
  37. {
  38.   TDib ballDib(*::Application, TResId(IDB_BALL));
  39.   Palette   = new TPalette(ballDib);
  40.  
  41.   DisplayDC = new TDibDC;
  42.   BallDC    = new TDibDC;
  43.  
  44.   BallBitmap = new TDiBitmap(ballDib);
  45.   BallDC->SelectObject(*BallBitmap);
  46.   BallDC->StretchDIBits(TRect(0, 0, BallBitmap->Width(), BallBitmap->Height()),
  47.                         TRect(0, 0, BallBitmap->Width(), BallBitmap->Height()),
  48.                         ballDib);
  49.   XPosition = 0;
  50.   YPosition = 0;
  51.   XDelta = random(RandomMax) + AbsoluteDelta;
  52.   YDelta = random(RandomMax) + AbsoluteDelta;
  53.  
  54.   WaveAudio = new TMciWaveAudio("boing.wav");
  55. }
  56.  
  57. //
  58. // TSpeedBallWindow Cleanup with a valid HWindow.
  59. //
  60. void
  61. TSpeedBallWindow::CleanupWindow()
  62. {
  63.   delete ClientDC;
  64.   delete Palette;
  65.   delete BallDC;
  66.   delete DisplayDC;
  67.   delete BallBitmap;
  68.   delete DisplayBitmap;
  69.   delete WaveAudio;
  70. }
  71.  
  72.  
  73. //
  74. // TSpeedBallWindow Paint.
  75. // Activates Palette, then blit's current double buffer.
  76. //
  77. void
  78. TSpeedBallWindow::Paint(TDC& dc, bool, TRect&)
  79. {
  80.   dc.SelectObject(*Palette);
  81.   dc.RealizePalette();
  82.   DisplayDC->BitBltToScreen(dc, 0, 0,
  83.                             DisplayBitmap->Width(), DisplayBitmap->Height());
  84. }
  85.  
  86. //
  87. // TSpeedBallWindow IdleAction.
  88. // When nothing needs to be processed step the image then update the
  89. // display.
  90. //
  91. bool
  92. TSpeedBallWindow::IdleAction(long)
  93. {
  94.   // No need to call the base class because it does not do anything.
  95.   //
  96.   bool bounce = false;
  97.  
  98.   static uint32 timesCalled = 0;
  99.  
  100.   if (++timesCalled % Delay != 0)
  101.     return true;
  102.  
  103.   if (DisplayBitmap) {
  104.     if (BlitterBlocks)
  105.       DisplayBitmap->CopyBlt(XPosition, YPosition, *BallBitmap, 0, 0);
  106.     else
  107.       DisplayBitmap->SpriteBlt(XPosition, YPosition, *BallBitmap, 0, 0);
  108.  
  109.     DisplayDC->BitBltToScreen(*ClientDC, XPosition, YPosition, 110, 110, XPosition, YPosition);
  110.  
  111.     XPosition += XDelta;
  112.     YPosition += YDelta;
  113.  
  114.     if (XPosition > XLimit) {
  115.       XPosition = XLimit;
  116.       XDelta = -(random(RandomMax) + AbsoluteDelta);
  117.       bounce = true;
  118.     }
  119.  
  120.     if (XPosition < 0) {
  121.       XPosition = 0;
  122.       XDelta = random(RandomMax) + AbsoluteDelta;
  123.       bounce = true;
  124.     }
  125.  
  126.     if (YPosition > YLimit) {
  127.       YPosition = YLimit;
  128.       YDelta = -(random(RandomMax) + AbsoluteDelta);
  129.       bounce = true;
  130.     }
  131.  
  132.     if (YPosition < 0) {
  133.       YPosition = 0;
  134.       YDelta = random(RandomMax) + AbsoluteDelta;
  135.       bounce = true;
  136.     }
  137.  
  138.     if (bounce && EnableSound && WaveAudio) {
  139.       WaveAudio->Play(MCI_NOTIFY);
  140.     }
  141.   }
  142.  
  143.   return true;
  144. }
  145.  
  146.  
  147. //
  148. //
  149. //
  150. void
  151. TSpeedBallWindow::EvPaletteChanged(HWND hWndPalChg)
  152. {
  153.   if (hWndPalChg != GetHandle()) {
  154.     TClientDC clientDC(*this);
  155. #if defined(BI_PLAT_WIN16)
  156.     Palette->UnrealizeObject();
  157. #endif
  158.     clientDC.SelectObject(*Palette, true);
  159.     bool needsUpdate = clientDC.RealizePalette() > 0;
  160.     if (needsUpdate)
  161.       Invalidate(false);
  162.   }
  163. }
  164.  
  165.  
  166. //
  167. //
  168. //
  169. bool
  170. TSpeedBallWindow::EvQueryNewPalette()
  171. {
  172.   if (ClientDC && Palette) {
  173.     ClientDC->SelectObject(*Palette);
  174.     ClientDC->RealizePalette();
  175.   }
  176.   return true;
  177. }
  178.  
  179.  
  180. //
  181. // When the size changes create a new double buffer.
  182. //
  183. void
  184. TSpeedBallWindow::EvSize(uint, TSize& newSize)
  185. {
  186.   if (DisplayDC && newSize.cx && newSize.cy) {
  187.     TDiBitmap* newBitmap = new TDiBitmap(newSize.cx, newSize.cy, 256);
  188.     DisplayDC->SelectObject(*newBitmap);
  189.  
  190.     delete DisplayBitmap;
  191.     DisplayBitmap = newBitmap;
  192.  
  193.     DisplayDC->PatBlt(0, 0, newSize.cx, newSize.cy, BLACKNESS);
  194.     DisplayDC->BitBltToScreen(*ClientDC, 0, 0, newSize.cx, newSize.cy);
  195.  
  196.     XLimit = newSize.cx - BallBitmap->Width();
  197.     YLimit = newSize.cy - BallBitmap->Height();
  198.   }
  199. }
  200.  
  201. //
  202. // TSpeedBallWindow SetupWindow.
  203. // Create WinG DC's, Bitmaps and double buffers.
  204. // Select objects into appropriate DC's.
  205. //
  206. void
  207. TSpeedBallWindow::SetupWindow()
  208. {
  209.   TWindow::SetupWindow();
  210.  
  211.   ClientDC = new TClientDC(*this);
  212. }
  213.  
  214.  
  215. //
  216. // TAboutWindow constructor
  217. //
  218. TAboutWindow::TAboutWindow(TWindow* parent)
  219. :
  220.   TLayoutWindow(parent, 0)
  221. {
  222.   const int SpeedBallPercent = 90;
  223.  
  224.   // Create the child controls.
  225.   // The WinG bouncing ball takes up 90 percent of the parent's height
  226.   //
  227.   SpeedBall = new TSpeedBallWindow(this);
  228.   TLayoutMetrics lmSpeedBall;
  229.   lmSpeedBall.X.SameAs(lmParent, lmLeft);
  230.   lmSpeedBall.Y.SameAs(lmParent, lmTop);
  231.   lmSpeedBall.Width.SameAs(lmParent, lmWidth);
  232.   lmSpeedBall.Height.PercentOf(lmParent, SpeedBallPercent);
  233.   SetChildLayoutMetrics(*SpeedBall, lmSpeedBall);
  234.  
  235.   // Ok button is 10 percent of parent's height and 1/3 its width.
  236.   //
  237.   Ok = new TButton(this, IDOK, "O&k", 10, 10, 10, 10, true);
  238.   TLayoutMetrics lmOk;
  239.   lmOk.X.SameAs(lmParent, lmLeft);
  240.   lmOk.Y.Below(SpeedBall, 1);
  241.   lmOk.Width.PercentOf(lmParent, 33);
  242.   lmOk.Height.PercentOf(lmParent, 100 - SpeedBallPercent);
  243.   SetChildLayoutMetrics(*Ok, lmOk);
  244.  
  245.   // Help button is same size as the Ok button but to the right of it
  246.   //
  247.   Help = new TButton(this, IDHELP, "&Help", 10, 10, 10, 10);
  248.   TLayoutMetrics lmHelp;
  249.   lmHelp.X.RightOf(Ok, 1);
  250.   lmHelp.Y.Below(SpeedBall, 1);
  251.   lmHelp.Width.SameAs(Ok);
  252.   lmHelp.Height.SameAs(Ok);
  253.   SetChildLayoutMetrics(*Help, lmHelp);
  254.  
  255.   // Option button is same size as the Ok button but to the right of
  256.   // the help button.
  257.   //
  258.   Options = new TButton(this, OPTIONSID, "&Options", 10, 10, 10, 10);
  259.   TLayoutMetrics lmOptions;
  260.   lmOptions.X.RightOf(Help, 1);
  261.   lmOptions.Y.Below(SpeedBall, 1);
  262.   lmOptions.Width.SameAs(Ok);
  263.   lmOptions.Height.SameAs(Ok);
  264.   SetChildLayoutMetrics(*Options, lmOptions);
  265.  
  266.   SetAcceleratorTable(IDA_ABOUT);
  267.  
  268.   // Center the about window
  269.   //
  270.   Attr.Style = WS_POPUP | WS_THICKFRAME | WS_VISIBLE | WS_CAPTION;
  271.   Attr.X = TUIMetric::CxFullScreen / 4;
  272.   Attr.Y = TUIMetric::CyFullScreen / 4;
  273.   Attr.W = TUIMetric::CxFullScreen / 2;
  274.   Attr.H = TUIMetric::CyFullScreen / 2;
  275.   SetCaption("About Blazer");
  276.  
  277.   // Give it a context menu
  278.   //
  279.   TMenu contextMenu(*::Application, IDM_AWCONTEXTMENU);
  280.   AssignContextMenu(new TPopupMenu(contextMenu));
  281. }
  282.  
  283.  
  284. DEFINE_RESPONSE_TABLE1(TAboutWindow, TLayoutWindow)
  285.   EV_WM_GETMINMAXINFO,
  286.   EV_COMMAND(IDOK, CmOk),
  287.   EV_COMMAND(IDHELP, CmHelp),
  288.   EV_COMMAND(OPTIONSID, CmOptions),
  289. END_RESPONSE_TABLE;
  290.  
  291. DEFINE_HELPCONTEXT(TAboutWindow)
  292.   HCENTRY_MENU_AND_CONTROL(IDH_ABOUTOK,      IDOK,      IDOK),
  293.   HCENTRY_MENU_AND_CONTROL(IDH_ABOUTHELP,    IDHELP,    IDHELP),
  294.   HCENTRY_MENU_AND_CONTROL(IDH_ABOUTOPTIONS, OPTIONSID, OPTIONSID),
  295. END_HELPCONTEXT;
  296.  
  297. //
  298. // Make sure the smallest size the About window can be is twice the
  299. // size of the ball bitmap.
  300. //
  301. void
  302. TAboutWindow::EvGetMinMaxInfo(MINMAXINFO far& minmaxinfo)
  303. {
  304.   if (SpeedBall && SpeedBall->BallBitmap) {
  305.     TPoint p(SpeedBall->BallBitmap->Width() * 2, SpeedBall->BallBitmap->Height() * 2);
  306.     minmaxinfo.ptMinTrackSize = p;
  307.   }
  308.   else
  309.     TLayoutWindow::EvGetMinMaxInfo(minmaxinfo);
  310. }
  311.  
  312. //
  313. // Close the About window on Ok.
  314. //
  315. void
  316. TAboutWindow::CmOk()
  317. {
  318.   CloseWindow(IDOK);
  319. }
  320.  
  321.  
  322. //
  323. // Display the property sheet.
  324. //
  325. void
  326. TAboutWindow::CmOptions()
  327. {
  328.   // Create the property sheets.
  329.   //
  330.   TPropertySheet* ps = new TPropertySheet(this, "Options");
  331.   TBitBltPage* bitBltPage = new TBitBltPage(SpeedBall->BlitterBlocks,
  332.     SpeedBall->EnableSound, ps, IDD_BITBLT);
  333.   TSpeedPage* speedPage = new TSpeedPage(::RandomMax, ::AbsoluteDelta,
  334.     SpeedBall->Delay, ps, IDD_SPEED);
  335.  
  336.   // If user pressed Ok, save the values to affect the bouncing ball
  337.   //
  338.   if (ps->Execute() == IDOK) {
  339.     ::RandomMax      = speedPage->FactorValue;
  340.     ::AbsoluteDelta  = speedPage->DeltaValue;
  341.     SpeedBall->Delay = speedPage->DelayValue;
  342.     SpeedBall->BlitterBlocks = bitBltPage->BlitterBlocks;
  343.     SpeedBall->EnableSound = bitBltPage->EnableSound;
  344.   }
  345. }
  346.  
  347. //
  348. // User wants help.
  349. //
  350. void
  351. TAboutWindow::CmHelp()
  352. {
  353.   WinHelp(HELPFILENAME, HELP_CONTEXT, IDH_ABOUTWINDOW);
  354. }
  355.  
  356. //
  357. // SetupWindow()
  358. // Setup the context sensitive help.
  359. //
  360. void
  361. TAboutWindow::SetupWindow()
  362. {
  363.   TLayoutWindow::SetupWindow();
  364.   SETUP_HELPCONTEXT(TBlazerApp, TAboutWindow);
  365. }
  366.  
  367.  
  368. //
  369. // CleanupWindow()
  370. // Removes the context sensitive help info.
  371. //
  372. void
  373. TAboutWindow::CleanupWindow()
  374. {
  375.   CLEANUP_HELPCONTEXT(TBlazerApp, TAboutWindow);
  376.   TLayoutWindow::CleanupWindow();
  377. }
  378.